//
//  MarketAreaViewController.m
//  我的小汽车
//
//  Created by 邹壮壮29 on 15-1-8.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "MarketAreaViewController.h"
#import "AreaModel.h"


@interface MarketAreaViewController ()<UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate>

@end

@implementation MarketAreaViewController
- (void)dealloc{
    [_bigArray release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.bigArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self markArea];
    [self createMarketArea];

}
-(void)markArea{
    //根据路径获取整个文件所存储的内容
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //通过字符串进行截取,并返回数组
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    //遍历从数组中取出元素
    for (NSString *str in array) {
        //以开头判断是否为省
        if (![str hasPrefix:@" "]) {
            AreaModel *pro = [AreaModel areaModelWithName:str];
            [_bigArray addObject:pro];
           
        } else{
            AreaModel *city = [AreaModel areaModelWithName:str];
            AreaModel *cityArray = [_bigArray lastObject];
            [cityArray.areaArray addObject:city];
        }
    }
}
-(void)createMarketArea{
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = _mySearch;

    [self.view addSubview:_tableView];
    [self.view sendSubviewToBack:_tableView];
}
//分组个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [_bigArray count];
    
}
//获取分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   
    AreaModel *pro = [_bigArray objectAtIndex:section];
    NSString *title = pro.name;
    return title;
   
}
//判断每个分组中有多少行内容
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
        AreaModel *model = [_bigArray objectAtIndex:section];
        return [model.areaArray count];
   
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    AreaModel *model = [_bigArray objectAtIndex:indexPath.section];
    AreaModel *city = [model.areaArray objectAtIndex:indexPath.row];

    cell.textLabel.text = city.name;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //通知传值
    AreaModel *model = [_bigArray objectAtIndex:indexPath.section];
    AreaModel *city = [model.areaArray objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:city.name forKey:@"cityName"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changLeftButtonText" object:self userInfo:dic];
     //收回视图
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
      self.view.frame = CGRectMake(- self.view.frame.size.width, 0, self.view.frame.size.width, 0);
    } completion:^(BOOL finished) {
    //收回视图时移除该视图
        [self.mark removeFromSuperview];
        self.collection.userInteractionEnabled = YES;
    }];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
